﻿using Jizsoft.JFrame.Common;
using Jizsoft.JFrame.Identity.Domain.Strategies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jizsoft.JFrame.Identity.Domain.Services
{
    public class LoginService
    {
        ICryptoSecurityStrategy _cryptoSecurityStrategy = new CryptoSecurityStrategy();

        /// <summary>
        /// 使用手机号码登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public SingleResult<Data.LoginResult> LoginByAccount(string account, string password)
        {
            //if (!_validationStrategy.ValidatePhone(phone))
            //{
            //    // 无效的手机号码
            //    return new ServiceResult<LoginResult>(ErrorCode.Login_InvalidPhone);
            //}

            //if (!_validationStrategy.ValidatePassword(password))
            //{
            //    // 无效的密码
            //    return new ServiceResult<LoginResult>(ErrorCode.Login_InvalidPassword);
            //}

            using (JFDbContext dbContext = new JFDbContext())
            {
                Entities.User user = dbContext.Set<Entities.User>().SingleOrDefault(p => p.Account == account);
                if (user == null)
                {
                    // 账号不存在
                    return new SingleResult<Data.LoginResult>(1, null);
                }

                if (user.Password != Md5Util.ToMd5(password))
                {
                    // 密码错误，密码保护策略执行操作
                    LockResult lockResult = _cryptoSecurityStrategy.IncorrectPassword(dbContext, user);

                    // 提交更改
                    if (dbContext.SaveChanges() > 0)
                    {
                        Data.LoginResult result = new Data.LoginResult();
                        if (!lockResult.IsLocked)
                        {
                            // 账号未锁定
                            result.RetryCount = lockResult.RetryCount;
                            result.UserId = 0;
                            result.Mobile = string.Empty;
                            result.Name = string.Empty;
                            return new SingleResult<Data.LoginResult>(false, 1, string.Empty, result);
                        }
                        else
                        {
                            // 账号已锁定
                            result.RetryCount = lockResult.RetryCount;
                            result.UserId = 0;
                            result.Mobile = string.Empty;
                            result.Name = string.Empty;
                            result.LockoutDuration = lockResult.LockoutDuration;
                        }
                        return new SingleResult<Data.LoginResult>(false, 1, string.Empty, result);
                    }
                    return new SingleResult<Data.LoginResult>(2, string.Empty);
                }
                else
                {
                    if (user.Status == Constant.UserStatus.Locked && DateTime.Now < user.UnlockTime)
                    {
                        // 账号已锁定，并且在锁定时间内
                        Data.LoginResult result = new Data.LoginResult();
                        result.RetryCount = 0;
                        result.UserId = 0;
                        result.Mobile = string.Empty;
                        result.Name = string.Empty;
                        result.IsLocked = true;
                        result.LockoutDuration = (user.UnlockTime.Value - DateTime.Now).Minutes;
                        return new SingleResult<Data.LoginResult>(false, 1, string.Empty, result);
                    }
                    else
                    {
                        // 账号登录成功
                        _cryptoSecurityStrategy.CorrectPassword(dbContext, user);

                        // 提交更改
                        if (dbContext.SaveChanges() > 0)
                        {
                            Data.LoginResult result = new Data.LoginResult();
                            result.UserId = user.Id;
                            result.Mobile = user.Mobile;
                            result.Name = user.Name;
                            result.RetryCount = 0;
                            result.IsLocked = false;
                            if (user.Gender != null)
                            {
                                result.Gender = user.Gender.Value;
                            }
                            result.NickName = user.NickName;
                            result.City = user.City;
                            if (!string.IsNullOrEmpty(user.AvatarUrl))
                            {
                                result.AvatarUrl = user.AvatarUrl;
                            }
                            else
                            {
                                result.AvatarUrl = string.Empty;// Configs.DefaultAvatarUrl;
                            }
                            return new SingleResult<Data.LoginResult>(result);
                        }
                        return new SingleResult<Data.LoginResult>(2, string.Empty);
                    }
                }
            }
        }
    }
}
